$(function() {
	$('.modal').modal({
		backdrop : 'static',
		show : false
	});

	$('.advert-thumb').each(function(index) {
		$(this).attr('data-index', index);
	});

	var $bannerAdEditor = $('#banner-advert-edit');
	
	
	$('#newsad-list').on('click','.news-delete-btn',function(){
		deleteNewsAd($(this).attr('id'));
		return false;
	});
	$('#newsad-list').on('click','.btn-primary-edit',function(){
		editNewsAd($(this).attr('id'));
		return false;
	});
	
	function BanAdEditor($ele) {
		var self = this;
		this.$ele = $ele;
		$ele.on('hide.bs.modal', function() {
			self.setData({
				image : '',
				linkUrl : '',
				content : ''
			});
		});
		$ele.find('[data-role="save-btn"]').on('click', function() {
			self['saveCallback'] && self['saveCallback']();
		});
	}

	BanAdEditor.prototype = {
		constructor : BanAdEditor,
		$ele : null,
		data : null,
		saveCallback : null,
		getData : function() {
			var $ele = this.$ele;
			var result = {
				image : $ele.find('img').attr('src'),
				linkUrl : $ele.find('[data-role="link-url"]').val(),
				content : $ele.find('[data-role="content"]').val(),
				sort : this.data['sort'],
				categoryId : 1
			}
			return result;
		},
		setData : function(obj) {
			var $ele = this.$ele;
			this.data = obj;
			$ele.find('img').attr('src', obj['image']), $ele.find(
					'[data-role="link-url"]').val(obj['linkUrl']), $ele.find(
					'[data-role="content"]').val(obj['content'])
		},
		show : function() {
			this.$ele.modal('show');
		},
		hide : function() {
			this.$ele.modal('hide');
		}
	}

	var editor = new BanAdEditor($('#banner-advert-edit'));

	$('.advert-thumb').on(
			'click',
			function() {
				var $this = $(this);
				editor.saveCallback = function() {
					var data = {};
					$.extend(data, editor.getData(), {
						id : $this.attr('data-id')
					});
					cAjax({
						url : '/web/advert/saveAdvertItem',
						data : data,
						success : function(obj) {
							var data = obj['data'];
							$this.attr('data-id', data['id']);
							$this.css('background-image', 'url('
									+ data['image'] + ')');
							editor.hide();
						}
					});
				}
				if ($this.attr('data-id') != '0') {
					cAjax({
						url : '/web/category/getItem',
						type : 'get',
						data : {
							itemId : $this.attr('data-id')
						},
						success : function(obj) {
							editor.setData(obj['data']);
							editor.show();
						}
					});
				} else {
					editor.setData({
						sort : $this.attr('data-index'),

					});
					editor.show();
				}
			});

	$('#pic-container,#newsad_pic-container a').tooltip({
		trigger : 'manual'
	}).tooltip('show');

	$('a.thumbnail').on('click', function() {
		var $this = $(this);
		var uploader = new PicUploader({
			size:'l',
			success : function(obj) {
				$this.find('img').attr('src', obj['data']);
			}
		});
		uploader.start();
	});

	// delete news ad
	function deleteNewsAd(id) {
		actionConfirm({	
			msg:"确认要删除该模块吗？",
			confirm:function(){
				cAjax({
					url : "/web/category/deleteItem",
					data : {
						itemId : id
					},
					success : function(data) {
						if (data.result) {
							greeting({
								msg : "删除成功！",
								confirm : function() {
									location.reload();
								}
							});
						} else {
							greeting({
								msg : "<h4 style=\"color:red\">失败，有错误！</h4>",
								title : "操作失败"
							});
						}
					}
				});
			}
		});
	}

	// edit news ad
	function editNewsAd(id) {

		cAjax({
			type : "get",
			url : "/web/category/getItem",
			data : {
				itemId : id
			},
			success : function(data) {
				if (data.result) {
					$("#itemId").val(data.data.id);
					$("#title").val(data.data.title);
					$("#content").val(data.data.content);
					$("#linkUrl").val(data.data.linkUrl);
					$("#newsad_pic-container").find('img').attr('src',
							data.data.image);
					$("#newsad_sort").val(data.data.sort);

					$('#newsAdModal').modal();
				} else {
					greeting({
						msg : "<h4 style=\"color:red\">失败，有错误！</h4>",
						title : "操作失败"
					});
				}
			}
		});

	}

	// 资讯视频保存
	$("#save_video_advert").click(function() {
		var linkUrl = $("#video_link_url").val();
		var id = $("#video_id").val();
		var categoryId = $("#video_category_id").val();
		if (linkUrl.trim() == "") {
			greeting({
				msg : "<h4 style=\"color:red\">亲！不能为空哦！</h4>",
				title : "操作失败"
			});
			return false;
		}

		cAjax({
			url : "/web/advert/saveAdvertItem",
			data : {
				id : id,
				linkUrl : linkUrl,
				categoryId : categoryId
			},
			success : function(data) {
				if (data.result) {
					greeting({
						msg : "保存成功！",
						confirm : function() {
							location.reload();
						}
					});
				} else {
					greeting({
						msg : "<h4 style=\"color:red\">失败，有错误！</h4>",
						title : "操作失败"
					});
				}
			}
		});

	});

	// open add news ad
	$("#add-newsad-btn").click(function() {
		var size = $("#newsad-list").children().length;
		if (size >= 5) {
			greeting({
				msg : "<h4 style=\"color:red\">亲，最多能添加5个哦！</h4>",
				title : "操作失败"
			});
			return false;
		}
		$("#newsad_form")[0].reset();
		$("#newsad_pic-container").find('img').attr('src', '');
		$('#newsAdModal').modal();
	});

	$("#save_news_ad").click(function() {
		var id = $("#itemId").val();
		var title = $("#title").val();
		var content = $("#content").val();
		var linkUrl = $("#linkUrl").val();
		var categoryId = $("#categoryId").val();
		var image = $("#newsad_pic-container").find('img').attr('src');
		var sort = $("#newsad_sort").val();

		if (!/^\d+$/.test(sort)) {
			greeting({
				msg : "<h4 style=\"color:red\">亲！排序应为正整数值！</h4>",
				title : "操作失败"
			});
			return false;
		}

		cAjax({
			url : "/web/advert/saveAdvertItem",
			data : {
				id : id,
				title : title,
				content : content,
				linkUrl : linkUrl,
				categoryId : categoryId,
				image : image,
				sort : sort
			},
			success : function(data) {
				if (data.result) {
					$('#newsAdModal').modal('hide');
					greeting({
						msg : "保存成功！",
						confirm : function() {
							location.reload();
						}
					});
				} else {
					greeting({
						msg : "<h4 style=\"color:red\">失败，有错误！</h4>",
						title : "操作失败"
					});
				}
			}
		});

	});

	$('#newsad-list').sortable({
		containment: $('#newsad-list'),
		appendTo: $('#newsad-list'),
		helper: 'clone',
		holder: '.dragger'
	});

});
